<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    //目标：{name:"zs",age:20} 转为 name=zs&age=20
    //核心思想：循环对象，取出每个key和value,再进行字符串拼接
    var obj = {name:"zs",age:20};
    var str = '';
    for(var key in obj){
      str += key+'='+obj[key]+'&'
    }
    str = str.slice(0,str.length-1);
    console.log(str); //name=zs&age=20
  </script>

  <script>
    //封装成一个方法（将对象转字符串的代码）方便调用
 
    function params(json_obj){
      var str = '';
      for(var key in json_obj){
        str += key + '=' + json_obj[key] + '&';
      }
      str = str.slice(0,str.length-1);
      return str;
    }
    console.log(params({"name":"zs","age":20})); //name=zs&age=20
    console.log(params({id:1001,type:'电视'})); //id=1001&type=电视
  </script>
</body>
</html>